﻿using System;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using wt.wine.Application.Common.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace WebAPI.Filters
{
    /// <summary>
    /// 登录校验
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public class LoginFilter : AuthorizeAttribute, IAuthorizationRequirement, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            var svc = context.HttpContext.RequestServices;
            var currentuserservices = svc.GetRequiredService<ICurrentUserService>();
            int userid = currentuserservices.GetUserId();
            if (userid <= 0)
            {
                context.Result = new ObjectResult("Invalidate value") { StatusCode = 401 };
                return;
            }
        }
    }
}
